<?php
/**
 * Created by PhpStorm.
 * User: Sun Jian
 * Date: 2019/2/1
 * Time: 14:28
 */

namespace ms\controllers;


use common\models\DhAddress;
use common\models\DhGoods;
use common\models\DhOrder;
use common\models\DhOrderDetail;
use common\models\DhProjectTel;
use common\models\DhUser;
use yii\web\Controller;
use Yii;

class ApiUserController  extends Controller
{
    public $data;
    public $uid;

    public function __construct($id,$module, array $config = [])
    {
        //校验seesion
        $request = Yii::$app->request;
        $this->data = json_decode(file_get_contents("php://input"),true);
        $openid = $this->data['openid'];
        $session = $this->data['session'];
        $user = DhUser::find()->where(['openid'=>$openid,'session_code'=>$session])->one();
        if(!$user){
           $return['status']=false;
           $return['code']=10001;
           $return['msg']="登陆过期";
           echo json_encode($return);
           return ;
        }else{
            $this->uid=$user->id;
        }

        parent::__construct($id, $module, $config);
    }

    public function actionGetAddressList(){
        $list = DhAddress::find()->where(['uid'=>$this->uid])->asArray()->all();
        $return = [];
        $return['status'] =true;
        $return['data']=$list;
        echo json_encode($return);
        return ;
    }

    public function actionGetAddressById(){
        if($this->data['id']==0){
            $data = DhAddress::find()->where(['uid'=>$this->uid])->asArray()->one();
        }else{
            $data = DhAddress::find()->where(['uid'=>$this->uid,'id'=>$this->data['id']])->asArray()->one();
        }

        $return = [];
        if($data){
            $return['status']=true;
            $return['data']=$data;
            echo json_encode($return);
            return ;
        }else{
            $return['status']=false;
            echo json_encode($return);
            return ;
        }
    }

    public function actionAddAddress(){
        $name= $this->data['name'];
        $tel = $this->data['tel'];
        $region = $this->data['region'];
        $address = $this->data['detail'];
        if($this->data['id']){
            $model = DhAddress::findOne($this->data['id']);
        }else{
            $model = new DhAddress();
        }

        $model->uid=$this->uid;
        $model->tel=$tel;
        $model->name=$name;
        $model->province=$region[0];
        $model->city=$region[1];
        $model->block=$region[2];
        $model->detail=$address;
        $return = [];
        if($model->save()){
            $return['status']=true;
            echo json_encode($return);
            return ;
        }else{
            $return['status']=false;
            $return['msg']=json_encode($model->errors);
            echo json_encode($return);
            return ;
        }
    }
    public function actionDelAddressById(){
        $data = DhAddress::find()->where(['uid'=>$this->uid,'id'=>$this->data['id']])->one();
        $data->delete();
        $return['status']=true;
        echo json_encode($return);
        return ;

    }
    public function actionAddOrder(){

        $add = DhAddress::find()->where(['uid'=>$this->uid,'id'=>$this->data['address']])->asArray()->one();

        $cart = $this->data['cart'];

        $return = [];
        $model = new DhOrder();
        $detail = new DhOrderDetail();
        $orderId = date("Ymdhis").$this->uid;
        $sum = 0;
        foreach ($cart as $v){
            $goods = DhGoods::findOne($v['id']);
            $_detail = clone $detail;
            $_detail->g_id=$goods->id;
            $_detail->g_name=$goods->name;
            $_detail->price=$goods->price;
            $_detail->img_id=$goods->img_id;
            $_detail->num=$v['num'];
            $_detail->orderId=$orderId;
            $_detail->create_time=time();
            $_detail->save();
            $sum += $goods->price * $v['num'];
            $model->img_id=$goods->img_id;
        }
        $model->orderId=$orderId;
        $model->create_time=time();
        $model->uid=$this->uid;
        $model->name = $add['name'];
        $model->tel = $add['tel'];
        $model->address=$add['province'].$add['city'].$add['block'].$add['detail'];
        $model->sum=$sum;

       if( $model->save()){
           $return['status']=true;
           echo json_encode($return);
           return ;
       }else{
           DhOrderDetail::deleteAll(['orderId'=>$orderId]);
           $return['status']=false;
           $return['msg']=json_encode($model->errors);
           echo json_encode($return);
           return ;
       }


    }

    public function actionGetOrdersByStatus(){
        if($this->data['status']==0){
            $list = DhOrder::find()->where(['uid'=>$this->uid])->asArray()->all();
        }else{
            $list = DhOrder::find()->where(['uid'=>$this->uid,'flag'=>$this->data['status']])->asArray()->all();
        }

        foreach ($list as $k=>$v){
            //查询每个订单商品
            $orders = [];
            $detail=DhOrderDetail::find()->where(["orderId"=>$v['orderId']])->all();

            foreach ($detail as $kk=> $vv){
                $orders[$kk]['img']=$vv->img;
                $orders[$kk]['name']=$vv->g_name;
                $orders[$kk]['gid']=$vv->g_id;
                $orders[$kk]['price']=$vv->price;
                $orders[$kk]['num']=$vv->num;
            }
            $list[$k]['addTime']=date("Y-m-d h:i:s");
            $status = "待付款";
            switch ($v['flag']){
                case 1:
                    $status="待付款";
                    break;
                case 2:
                    $status="已付款";
                    break;
                case 3:
                    $status="已发货";
                    break;
                case 4:
                    $status="已完成";
                    break;
                case 5:
                    $status="已取消";
                    break;
                default:
                    $status="未知状态";

            }
            $list[$k]['status']=$status;
            $list[$k]['list']=$orders;
        }
        $return['status']=true;
        $return['data']=$list;
        echo json_encode($return);
        return ;
    }

    public function actionGetOrderById(){

        $list = DhOrder::find()->where(['uid'=>$this->uid,'id'=>$this->data['id']])->asArray()->one();

            //查询每个订单商品
            $orders = [];
            $detail=DhOrderDetail::find()->where(["orderId"=>$list['orderId']])->all();

            foreach ($detail as $kk=> $vv){
                $orders[$kk]['img']=$vv->img;
                $orders[$kk]['name']=$vv->g_name;
                $orders[$kk]['gid']=$vv->g_id;
                $orders[$kk]['price']=$vv->price;
                $orders[$kk]['num']=$vv->num;
            }
            $list['addTime']=date("Y-m-d h:i:s");
            $status = "待付款";
            switch ($list['flag']){
                case 1:
                    $status="待付款";
                    break;
                case 2:
                    $status="已付款";
                    break;
                case 3:
                    $status="已发货";
                    break;
                case 4:
                    $status="已完成";
                    break;
                case 5:
                    $status="已取消";
                    break;
                default:
                    $status="未知状态";

            }
            $list['status']=$status;
            $list['list']=$orders;

        $return['status']=true;
        $return['data']=$list;
        echo json_encode($return);
        return ;
    }

    public function actionProject(){
        $model = new DhProjectTel();
        $model->uid=$this->uid;
        $model->p_id=$this->data['pid'];
        $model->phone=$this->data['tel'];
        $model->num=$this->data['num'];
        if($model->save()){
            $return['status']=true;
            echo json_encode($return);
            return ;
        }else{
            $return['status']=false;
            $return['msg']=json_encode($model->errors);
            echo json_encode($return);
            return ;
        }
    }
}